
#include <config.h>
#include <encoding_manager.h>
#include <string.h>

static int Utf16beIsSupport(unsigned char *pucBufferHead);
static int Utf16beGetCodeFrmBuffer(unsigned char *pucBufferStart, unsigned char *pucBufferEnd, unsigned int *pdwCode);

static T_EncodingOpr g_tUtf16beEncodingOpr = {
	.name             = "utf-16be",
	.iHead            = 2,
	.IsSupport        = Utf16beIsSupport,
	.GetCodeFrmBuffer = Utf16beGetCodeFrmBuffer,
};

static int Utf16beIsSupport(unsigned char *pucBufferHead)
{
	const char aUtf16beHead[] = {0xFE, 0xFF, 0};

	if(strncmp(aUtf16beHead, (const char *)pucBufferHead, 2) == 0)
		return 1;
	else
		return 0;
}

static int Utf16beGetCodeFrmBuffer(unsigned char *pucBufferStart, unsigned char *pucBufferEnd, unsigned int *pdwCode)
{
	if(pucBufferStart + 1 < pucBufferEnd)
	{
		*pdwCode = ((unsigned int)(pucBufferStart[0]<<8)) + pucBufferStart[1];
		return 2;
	}
	else
	{
		return 0;
	}
}

int Utf16beEncodingRegister(void)
{	
	AddFontOprForEncoding(&g_tUtf16beEncodingOpr, GetFontOpr("freetype"));
	AddFontOprForEncoding(&g_tUtf16beEncodingOpr, GetFontOpr("ascii"));	
	return RegisterEncodingOpr(&g_tUtf16beEncodingOpr);
}


